package cn.mysite.mycompany.service.impl;

import cn.mysite.mycompany.entity.Product;
import cn.mysite.mycompany.entity.ProductCategory;
import cn.mysite.mycompany.mapper.ProductCategoryMapper;
import cn.mysite.mycompany.mapper.ProductMapper;
import cn.mysite.mycompany.service.ProductCategoryService;
import cn.mysite.mycompany.vo.ProductCategoryVo;
import cn.mysite.mycompany.vo.ProductVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author  ChenChongFei
 * @since 2021-03-13
 */
@Service

public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductCategoryVo> getAllProductCategoryVo() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq( "type " ,1);
        List<ProductCategory> levelOne = productCategoryMapper.selectList(wrapper);
        List<ProductCategoryVo> levelOneVO = levelOne.stream().map(e -> new ProductCategoryVo(e.getId() ,e.getName())).collect(Collectors.toList());

        for (int i=0;i<levelOneVO.size();i++) {
            levelOneVO.get(i).setBannerImg("/images/banner"+i+".png");
            levelOneVO.get(i).setTopImg("/images/top"+i+".png");
            //商品信息赋值
            wrapper = new QueryWrapper();
            wrapper.eq( "categorylevelone_id " ,levelOneVO.get(i).getId());
            List<Product> productList=productMapper.selectList(wrapper);
            List<ProductVo> productVoList=productList.stream()
                    .map(e ->new ProductVo(
                            e.getId(),
                            e.getName(),
                            e.getPrice(),
                            e.getFileName()
            )).collect(Collectors.toList());
            levelOneVO.get(i).setProduct(productVoList);
        }

        for (ProductCategoryVo level0neProductCategoryV0 : levelOneVO){
            wrapper = new QueryWrapper();
            wrapper.eq("type ",2);
            wrapper.eq("parent_id" ,level0neProductCategoryV0.getId());
            List<ProductCategory> levelTwo = productCategoryMapper.selectList(wrapper);
            List<ProductCategoryVo> levelTwoVO = levelTwo.stream().map(e -> new ProductCategoryVo(e.getId(),e.getName())).collect(Collectors.toList());
            level0neProductCategoryV0.setChildren(levelTwoVO);
            for (ProductCategoryVo levelTwoProducCategorytVo : levelTwoVO) {
                wrapper = new QueryWrapper();
                wrapper.eq("type ", 3);
                wrapper.eq("parent_id",  levelTwoProducCategorytVo.getId());
                List<ProductCategory> levelThree = productCategoryMapper.selectList(wrapper);
                List<ProductCategoryVo> leveThreeVo = levelThree.stream().map(e -> new ProductCategoryVo(e.getId(), e.getName())).collect(Collectors.toList());
                levelTwoProducCategorytVo.setChildren(leveThreeVo);
            }
        }
        return levelOneVO;
    }
}

